package com.example.meitao.ui;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.meitao.R;
import com.example.meitao.view.MyMessage;

public class PrivateActivity extends AppCompatActivity implements View.OnClickListener {

    private MyMessage change_pwd,change_name,address;
    private ImageView iv_back;

    private long exitTime = 0;

    private Boolean isfinished =false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_private);
        initView();
        setListener();
    }
  /*  //设置返回
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_HOME) {
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }*/
    //设置再按一次退出返回键
    @Override
    public void onBackPressed()
    {
        isCanBack();

    }
    //设置再按一次退出
    public void isCanBack() {
        //super.onBackPressed();
        if (isfinished)
        {
            finish();
        } else
        {
            Toast.makeText(this, "再按一次返回键退出", Toast.LENGTH_SHORT).show();
            new Thread()
            {
                public void run()
                {
                    isfinished = true;
                    try
                    {
                        Thread.sleep(2000);
                        isfinished = false;
                    } catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                };
            }.start();
        }
    }

    private void setListener() {
        change_pwd.setOnClickListener(this);
        change_name.setOnClickListener(this);
        address.setOnClickListener(this);
        iv_back.setOnClickListener(this);



    }


    private void initView() {
        //隐藏actionbar
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            supportActionBar.hide();
        }
        change_pwd = (MyMessage) findViewById(R.id.view_private_change_pwd);
        change_name = (MyMessage) findViewById(R.id.view_private_change_name);
        address = (MyMessage) findViewById(R.id.view_private_address);
        iv_back = (ImageView) findViewById(R.id.iv_private_back);

    }

    @Override
    public void onClick(View v) {

        AlertDialog.Builder builder =new AlertDialog.Builder(this);
        switch (v.getId()) {
            case R.id.view_private_change_pwd :
                View view =getLayoutInflater().inflate(R.layout.dialog_pwd,null);
                final EditText etPwd = (EditText) view.findViewById(R.id.et_change_pwd);
                final EditText etNewPwd = (EditText) view.findViewById(R.id.et_change_newpwd);

                builder.setView(view).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        String pwd =etPwd.getText().toString();
                        String name =etNewPwd.getText().toString();
                        Toast.makeText(PrivateActivity.this, "点击了修改密码", Toast.LENGTH_SHORT).show();
                    }
                }).create().show();
                break;

            case R.id.view_private_change_name:
                View viewname = getLayoutInflater().inflate(R.layout.dialog_name,null);
                final  EditText Name = (EditText) viewname.findViewById(R.id.et_change_name);
                builder.setView(viewname).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String name =Name.getText().toString();
                        Toast.makeText(PrivateActivity.this, "点击了修改姓名", Toast.LENGTH_SHORT).show();

                    }
                }).create().show();


                break;
            case R.id.view_private_address:
                startActivity(new Intent(PrivateActivity.this,AddressActivity.class));
                Toast.makeText(this, "点击了收货地址", Toast.LENGTH_SHORT).show();

                break;
            case R.id.iv_private_back:
                finish();
                break;

        }
    }
}
